package zhr.chapter10;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Example10_2 {
    public static void main(String[] args) {
        File helloFile=new File("d://","HelloWorld.txt");

        try {
            //创建文件
            boolean isCreated =helloFile.createNewFile();
            if(isCreated||helloFile.exists()){
                //以字节流的方式写入文件helloFile
                FileOutputStream out = new FileOutputStream(helloFile);
                byte[] b= "Java 面向对象程序设计教程".getBytes();
                out.write(b,0,b.length);
                out.close();
                //以字节流的方式读取文件heloFile
                FileInputStream input=new FileInputStream(helloFile);
                int end;
                while ((end = input.read(b,0,b.length))!=-1){
                    String s=new String(b,0,end,"GBK");
                    System.out.println(s);
                }
                input.close();
            }
        } catch (IOException e) {
            Logger.getLogger(Example10_2.class.getName()).log(Level.SEVERE,null,e);
        }

    }
}
